package br.usp.ime.protoc.client;

import java.util.Iterator;
import java.util.List;

import br.usp.ime.protoc.pessoa.Paciente;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ConsultaPaciente extends Composite {

    private static Grid grid;

    public ConsultaPaciente() {

        final VerticalPanel verticalPanel = new VerticalPanel();
        initWidget(verticalPanel);
        verticalPanel.setBorderWidth(1);

        final Label dadosCadastraisPacienteLabel = new Label("Consula de Pacientes");
        verticalPanel.add(dadosCadastraisPacienteLabel);
        dadosCadastraisPacienteLabel.setStylePrimaryName("gwt-Label-Title");
        dadosCadastraisPacienteLabel.setStyleName("gwt-Label-Title");

        {
            grid = new Grid();
            verticalPanel.add(grid);
            grid.addTableListener(new TableListener() {
                public void onCellClicked(final SourcesTableEvents sender, final int row, final int cell) {
                    
                }
            });
            grid.setBorderWidth(1);
            grid.resize(1, 6);
            {
                final Label idLabel = new Label("ID");
                grid.setWidget(0, 0, idLabel);
                idLabel.setStyleName("simple-personHeader");
            }
            {
                final Label nomeLabel = new Label("Nome");
                grid.setWidget(0, 1, nomeLabel);
                nomeLabel.setStyleName("simple-personHeader");
            }
            {
                final Label cpfLabel = new Label("Cpf");
                grid.setWidget(0, 2, cpfLabel);
                cpfLabel.setStyleName("simple-personHeader");
            }
            {
                final Label dataNascimentoLabel = new Label("Data de Nascimento");
                grid.setWidget(0, 3, dataNascimentoLabel);
                dataNascimentoLabel.setStyleName("simple-personHeader");
            }
            {
                final Label emailLabel = new Label("Email");
                grid.setWidget(0, 4, emailLabel);
                emailLabel.setStyleName("simple-personHeader");
            }

            getPacientes();
        }

        final FlexTable flexTable = new FlexTable();
        verticalPanel.add(flexTable);

        final Button cadastrarNovoPacienteButton = new Button();
        flexTable.setWidget(0, 0, cadastrarNovoPacienteButton);
        cadastrarNovoPacienteButton.addClickListener(new ClickListener() {

            public void onClick(final Widget sender) {
                final CadastroPaciente cadastroPaciente = new CadastroPaciente();
                flexTable.setWidget(2, 1, cadastroPaciente);

            }
        });
        cadastrarNovoPacienteButton.setText("Cadastrar Novo Paciente");
    }

    public static void getPacientes() {
        PacienteService.Util.getInstance().obterPacientes(new AsyncCallback() {

            public void onSuccess(Object result) {
                List<Paciente> pacientes = (List<Paciente>) result;
                grid.resize(1 + pacientes.size(), 7);
                int row = 1;
                for (Iterator<Paciente> I = pacientes.iterator(); I.hasNext(); row++) {
                    Paciente paciente = I.next();
                    final int idPaciente = paciente.getId();
                    grid.setWidget(row, 0, new Label(String.valueOf(paciente.getId())));
                    grid.setWidget(row, 1, new Label(paciente.getNome()));
                    grid.setWidget(row, 2, new Label(paciente.getCpf()));
                    grid.setWidget(row, 3, new Label(paciente.getDataNascimento()));
                    grid.setWidget(row, 4, new Label(paciente.getEmail()));

                    final Image image = new Image();
                    grid.setWidget(row, 5, image);
                    image.addClickListener(new ClickListener() {

                        public void onClick(final Widget sender) {
                            excluirPaciente(idPaciente);
                        }
                    });
                    image.setUrl("js/ext/resources/images/default/dd/drop-no.gif");
                    
                    final Image image_2 = new Image();
                    grid.setWidget(row, 6, image_2);
                    image_2.addClickListener(new ClickListener() {

                        public void onClick(final Widget sender) {
                            //alterarPaciente(idPaciente);
                        }
                    });
                    image_2.setUrl("js/ext/resources/images/default/dd/drop-no.gif");
                    
                }
            }

            public void onFailure(Throwable caught) {
                final Label mensagem = new Label("Problemas na consulta do Paciente - " + caught.getMessage() + "\n"
                        + caught.getCause());
                ProtocProman.simplePanelMsg.add(mensagem);
                // grid.setWidget(6, 0, mensagem);
            }
        });
    }

    private static void excluirPaciente(final int idPaciente) {
        PacienteService.Util.getInstance().excluirPaciente(idPaciente, new AsyncCallback() {

            public void onSuccess(Object result) {
                Boolean resultado = (Boolean) result;

                if (resultado) {
                    getPacientes();
                } else {
                    final Label mensagem = new Label("Problemas na exclusao do Controle");
                    ProtocProman.simplePanelMsg.add(mensagem);
                    // grid.setWidget(6, 0, mensagem);
                }
            }

            public void onFailure(Throwable caught) {
                final Label mensagem = new Label("Problemas na exclusao do Paciente - " + caught.getMessage() + "\n"
                        + caught.getCause());
                ProtocProman.simplePanelMsg.add(mensagem);
                // grid.setWidget(6, 0, mensagem);
            }
        });
    }

}
